<template>
  <div class="my-account-wrap">
    <div class="header">
      <div style="width: 30.94rem" v-if="isTuiguang">
        <div class="yaoqin">
          <p style="width: 20rem">{{ $t("邀请链接: ") }}</p>
          <p style="">{{ userInfo.inviterUrl || $t("无") }}</p>
        </div>
        <div
          class="btn yaoqin-btn"
          style="margin-top: -1.06rem"
          @click="copyToClipboard(userInfo.inviterUrl)"
        >
          {{ $t("复制链接") }}
        </div>
      </div>
      <div
        class="header-price"
        v-for="(item, index) in userInfo.brokerages"
        :key="index"
      >
        <p>
          {{ $t("可提账户")
          }}<span
            >{{ item.lang == "zh" ? "¥" : "$"
            }}{{ (item.brokeragePrice || 0) / 100 }}</span
          >
        </p>
        <div class="btn-wrap">
          <div class="btn" @click="tixian(item.lang)">{{ $t("提现") }}</div>
          <p class="btn-p" @click="checkWithList(item.lang)">
            {{ $t("提现记录") }}
          </p>
        </div>
      </div>
    </div>
    <p class="account-title">{{ $t("资金明细") }}</p>
    <div class="list">
      <div class="none-box" v-if="accountList.length == 0">
        {{ $t("暂无数据") }}
      </div>
      <div
        class="person-info-wrap"
        v-for="(item, index) in accountList"
        :key="index"
      >
        <div class="info">
          <div class="head-box">
            <img :src="item.sourceUserInfo?.avatar" alt="" />
          </div>
          <div class="info-box">
            <p class="name">
              <span>{{ item.sourceUserInfo?.nickname }}</span>
              <span>{{
                formatDate(item.createTime, "YYYY-MM-DD HH-mm-ss")
              }}</span>
            </p>
            <div class="tag">
              <span
                v-for="(citem, cindex) in item.sourceUserInfo?.groups"
                :key="cindex"
                >{{ citem }}</span
              >
            </div>
          </div>
        </div>
        <div class="price">
          <p>¥{{ item.price / 100 }}</p>
          <p>{{ item.title }}</p>
        </div>
      </div>
    </div>
    <div class="pagination-box">
      <el-pagination
        background
        :current-page="page.pageNo"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </div>
    <p class="account-title" v-if="isTuiguang">{{ $t("邀请规则") }}</p>
    <div class="guize" v-if="isTuiguang">{{ $t("邀请规则") }}</div>
    <withrawListDialog ref="withrawListDialog"></withrawListDialog>
    <tixianDialog ref="tixianDialog" @initUser="getUserInfo"></tixianDialog>
  </div>
</template>
<script>
import api from "@/api/personcenter.js";
import { formatDate } from "@/utils/index.js";
import withrawListDialog from "./withrawListDialog.vue";
import tixianDialog from "./tixianDialog.vue";
export default {
  components: {
    withrawListDialog,
    tixianDialog,
  },
  data() {
    return {
      page: {
        pageNo: 1,
        pageSize: 10,
        total: 0,
      },
      userInfo: {},
      accountList: [],
    };
  },
  computed: {
    isTuiguang() {
      return this.$store.getters.isTuiguang;
    },
  },

  mounted() {
    this.init();
    this.initUserInfo();
  },
  methods: {
    async getUserInfo() {
      let res = await this.$http.get(api.getUserInfo);
      if (res && res.code == 0) {
        this.$store.commit("SET_USER", res.data);
        this.initUserInfo();
      } else {
        this.$message.error(res.msg || this.$t("网络错误"));
      }
    },
    tixian(lang) {
      this.$refs["tixianDialog"].openDialog(lang);
    },
    checkWithList(lang) {
      this.$refs["withrawListDialog"].show(lang);
    },
    formatDate(time, format) {
      return formatDate(time, format);
    },
    async copyToClipboard(text) {
      // 创建临时textarea元素
      const textarea = document.createElement("textarea");
      textarea.value = text;
      textarea.style.position = "fixed"; // 防止页面滚动
      document.body.appendChild(textarea);

      // 选择文本
      textarea.select();
      textarea.setSelectionRange(0, 99999); // 对于移动设备

      try {
        // 执行复制命令
        // 现代API优先
        if (navigator.clipboard) {
          await navigator.clipboard.writeText(text);
        } else {
          // 回退方案
          document.execCommand("copy");
        }
        this.$message.success(this.$t("复制成功"));
        return true;
      } catch (err) {
        console.log(err);
        this.$message.error(this.$t("复制失败"));
        return false;
      } finally {
        // 清理DOM
        document.body.removeChild(textarea);
      }
    },
    initUserInfo() {
      let userInfo = this.$store.state.userInfo;
      if (userInfo) {
        this.userInfo = JSON.parse(userInfo);
        this.$forceUpdate();
      }
    },
    async init() {
      let res = await this.$http.get(api.getAccountList, {
        params: {
          pageNo: this.page.pageNo,
          pageSize: this.page.pageSize,
          status: 1,
        },
      });
      if (res && res.code == 0) {
        this.accountList = res.data.list;
        this.page.total = res.data.total;
      } else {
        this.$message.error(res.msg || this.$t("网络错误"));
      }
    },
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.page.pageNo = 1;
      this.init();
    },
    handleCurrentChange(val) {
      this.page.pageNo = val;
      this.init();
    },
  },
};
</script>
<style lang="scss" scoped>
.my-account-wrap {
  padding: 1.33rem 1.39rem;
  .header {
    display: flex;
    margin-bottom: 3.56rem;
    .yaoqin {
      display: flex;
      p:nth-child(2) {
        word-wrap: break-word;
        width: 24rem;
      }
    }
    & > div {
      height: 6.28rem;
      background: rgba(66, 95, 155, 0.05);
      border-radius: 0.11rem 0.11rem 0.11rem 0.11rem;
      border: 0.06rem solid #dcdfe6;
      display: flex;
      flex-direction: column;
      padding-left: 0.89rem;
      padding-top: 1.39rem;
      padding-right: 0.83rem;
      height: 6.28rem;
      margin-right: 0.89rem;
      & > p {
        font-size: 0.89rem;
        color: #333333;
        line-height: 1.22rem;
        letter-spacing: 1px;
      }
      .btn-wrap {
        display: flex;
        margin-top: 1.06rem;
      }
      .btn {
        width: 4rem;
        height: 1.56rem;
        background: #339eff;
        border-radius: 0.06rem 0.06rem 0.06rem 0.06rem;
        font-size: 0.67rem;
        color: #ffffff;
        text-align: center;
        line-height: 1.56rem;
        cursor: pointer;
      }
      .btn-p {
        font-size: 0.78rem;
        color: #339eff;
        line-height: 1.56rem;
        text-decoration: underline;
        margin-left: 0.89rem;
        cursor: pointer;
      }
    }
    .header-price {
      font-size: 1rem;
      span {
        color: rgba(248, 92, 2, 1);
        margin-left: 0.44rem;
      }
    }
  }
  .account-title {
    font-weight: bold;
    font-size: 1.33rem;
    color: #333333;
  }
  .list {
    .person-info-wrap {
      margin-bottom: 1rem;
      width: 100%;
      height: 6.28rem;
      background: rgba(66, 95, 155, 0.05);
      border-radius: 0.11rem 0.11rem 0.11rem 0.11rem;
      border: 0.06rem solid #dcdfe6;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0 1.39rem;
      .info {
        display: flex;
        align-items: center;
      }
      .head-box {
        width: 3.56rem;
        height: 3.56rem;
        margin-right: 0.89rem;
        border-radius: 2rem;
        overflow: hidden;
        img {
          width: 100%;
          height: 100%;
        }
      }
      .info-box {
        .name {
          display: flex;
          align-items: center;
          span:nth-child(1) {
            font-weight: bold;
            font-size: 1rem;
            color: #333333;
            line-height: 1.22rem;
            letter-spacing: 2px;
            margin-right: 0.89rem;
          }
          span:nth-child(2) {
            font-size: 0.78rem;
            color: #333333;
            line-height: 1.22rem;
            letter-spacing: 1px;
          }
        }
        .tag {
          span {
            line-height: 1.11rem;
            font-size: 0.67rem;
            color: #fff;
            padding: 0 0.12rem;
            margin-right: 0.22rem;
            background: #339694;
            border-radius: 0.06rem 0.06rem 0.06rem 0.06rem;
          }
        }
      }
      .price {
        font-size: 0.78rem;
        p:nth-child(1) {
          color: #f85c02;
          font-weight: bold;
          margin-bottom: 0.44rem;
        }
      }
    }
  }
  .pagination-box {
    text-align: center;
    margin-top: 1.44rem;
    margin-bottom: 3.56rem;
  }
  .guize {
    width: 100%;
    background: rgba(66, 95, 155, 0.05);
    border-radius: 0.11rem 0.11rem 0.11rem 0.11rem;
    border: 0.06rem solid #dcdfe6;
    font-size: 0.78rem;
    color: #333333;
    line-height: 1.22rem;
    letter-spacing: 1px;
    text-align: justify;
    padding: 1.11rem;
  }
}
</style>
<style lang="scss">
.my-account-wrap {
  .el-pagination.is-background .el-pager li {
    border-radius: 0.2rem;
    border: 0.06rem solid #d9dbe2;
    background: #ffffff;
  }
  .el-pagination.is-background .el-pager li:not(.disabled).active {
    border: 0.06rem solid #146eb4;
    color: #146eb4;
    background: #ffffff;
  }
}
</style>
